(import domotics.*)

 (deftemplate temp_out
    (slot value))
    
 (deftemplate time
 	(slot hours))
 	
 (deftemplate alarm
 	(slot state))
 
 	
(deftemplate Livingroom (declare (from-class Livingroom)))
(deftemplate Bedroom (declare (from-class Bedroom)))
(deftemplate Kitchen (declare (from-class Kitchen)))
(deftemplate WC (declare (from-class WC)))
(deftemplate Coffe (declare (from-class Appliance)))
(deftemplate Stereo (declare(from-class Appliance)))
(deftemplate Alarm  (declare(from-class Alarm)))


;==============================================================================================

(defrule stupid
	(printout t "lol" crlf)
	=>
	(printout t "wut1")
)

(defrule morning
	?coffe<-(Coffe(status ?x)) 
	?stereo<-(Stereo(status ?x))
	(time(hours ?x&:(<= ?x 10)&:(=> ?x 7)))
	=>
	(modify ?coffe (status "low"))
	(modify ?stereo (status "low"))
)

(defrule quiet
	?stereo<-(Stereo(status ?x))
	(time(hours ?x&:(<= ?x 24)&:(> ?x 22)&:(>= ?x 0)&:(< ?x 7)))
	=>
	(modify ?stereo (status "off"))
)

(defrule sound
	?stereo<-(Stereo(status ?x))
	(Livingroom{presence == FALSE})
	=>
	(modify ?stereo (status "off"))
	(printout t "Stereo now off." crlf)

)

(defrule warning
	(Alarm{status == "on" && detection == "positive"})
	(Livingroom{presence == FALSE})
	(Bedroom{pres == FALSE})
	(Kitchen{pres == FALSE})
	(WC{pres == FALSE})
	=>
	(printout t "Warning! Intruder alert" crlf)
)
	
;==============================================================================================


(defrule lrmildmild
	?temp <- (Livingroom (tempin ?x&:(<= ?x 22)&:(> ?x 20)))
	(temp_out(value ?y&:(<= ?y 22)&:(> ?y 20)))
	=>
	;(modify ?temp (acstat "off"))
	(printout t "checked" crlf)
)

;____________________________________________________________

(defrule lrmildhot
	?temp <- (Livingroom (tempin ?x&:(and (<= 22)(> 20))))
	(temp_out(value ?y&:(and(<= 25)(> 22))))
	=>
	(modify ?temp (ac off)(blind closed)(heat off))
)

(defrule lrmildcold
	?temp <- (Livingroom (tempin ?x&:(and (<= 22)(> 20))))
	(temp_out(value ?y&:(and(<= 20)(> 15))))
	=>
	(modify ?temp (ac off)(blind open)(heat off))
)

(defrule lrhotmild
	?temp <- (Livingroom (tempin ?x&:(and (<= 25)(> 22))))
	(temp_out(value ?y&:(and (<= 22)(> 20))))
	=>
	(modify ?temp (ac off)(blind open)(heat off))
)

(defrule lrhotcold
	?temp <- (Livingroom (tempin ?x&:(and (<= 25)(> 22))))
	(temp_out(value ?y&:(and (< 20)(> 15))))
	=>
	(modify ?temp (ac off)(blind open)(heat off))
)

(defrule lrhothot
	?temp <- (Livingroom (tempin ?x&:(and (<= 25)(> 22))))
	(temp_out(value ?y&:(and (<= 25)(> 22))))
	=>
	(modify ?temp (ac on)(blind closed)(heat off))
)

(defrule lrcoldcold
	?temp <- (Livingroom (tempin ?x&:(and (<= 20)(> 15))))
	(temp_out(value ?y&:(and (<= 20)(> 15))))
	=>
	(modify ?temp (ac off)(blind open)(heat on))
)

(defrule lrcoldmild
	?temp <- (Livingroom (tempin ?x&:(and (<= 15)(> 20))))
	(temp_out(value ?y&:(and (<= 22)(> 20))))
	=>
	(modify ?temp (ac off)(blind open)(heat on))
)

(defrule lrcoldhot
	?temp <- (Livingroom (tempin ?x&:(and (<= 20)(> 15))))
	(temp_out(value ?y&:(and (<= 25)(> 22))))
	=>
	(modify ?temp (ac off)(blind open)(heat on))
)

;=======================================================================================

(defrule brmildmild
	?temp <- (Bedroom (tempin ?x&:(and(< 22)(> 20))))
	(temp_out(value ?y&:(and (< 22)(> 20))))
	=>
	(modify ?temp (ac off)(blind closed)(heat off))
)

(defrule brmildhot
	?temp <- (Bedroom (tempin ?x&:(and(<= 22)(> 20))))
	(temp_out(value ?y&:(and (<= 25)(> 22))))
	=>
	(modify ?temp (ac off)(blind closed)(heat off))
)

(defrule brmildcold
	?temp <- (Bedroom (tempin ?x&:(and(<= 22)(> 20))))
	(temp_out(value ?y&:(and (<= 20)(> 15))))
	=>
	(modify ?temp (ac off)(blind open)(heat off))
)

(defrule brhotmild
	?temp <- (Bedroom (tempin ?x&:(and(<= 25)(> 22))))
	(temp_out(value ?y&:(and (<= 22)(> 20))))
	=>
	(modify ?temp (ac off)(blind open)(heat off))
)

(defrule brhotcold
	?temp <- (Bedroom (tempin ?x&:(and(<= 25)(> 22))))
	(temp_out(value ?y&:(and (< 20)(> 15))))
	=>
	(modify ?temp (ac off)(blind open)(heat off))
)

(defrule brhothot
	?temp <- (Bedroom (tempin ?x&:(and (<= 25)(> 22))))
	(temp_out(value ?y&:(and(<= 25)(> 22))))
	=>
	(modify ?temp (ac on)(blind closed)(heat off))
)

(defrule brcoldcold
	?temp <- (Bedroom (tempin ?x&:(and (<= 20)(> 15))))
	(temp_out(value ?y&:(and (<= 20)(> 15))))
	=>
	(modify ?temp (ac off)(blind open)(heat on))
)

(defrule brcoldmild
	?temp <- (Bedroom (tempin ?x&:(and (<= 15)(> 20))))
	(temp_out(value ?y&:(and(<= 22)(> 20))))
	=>
	(modify ?temp (ac off)(blind open)(heat on))
)

(defrule brcoldhot
	?temp <- (Bedroom (tempin ?x&:(and (<= 20)(> 15))))
	(temp_out(value ?y&:(and (<= 25)(> 22))))
	=>
	(modify ?temp (ac off)(blind open)(heat on))
)
;==============================================================================


(defrule ktmildmild
	?temp <- (Kitchen (temp ?x&:(and (< 22)(> 20))))
	(temp_out(value ?y&:(and (< 22)(> 20))))
	=>
	(modify ?temp (ac off)(blind closed)(heat off))
)

(defrule ktmildhot
	?temp <- (Kitchen (temp ?x&:(and (<= 22)(> 20))))
	(temp_out(value ?y&:(and (<= 25)(> 22))))
	=>
	(modify ?temp (ac off)(blind closed)(heat off))
)

(defrule ktmildcold
	?temp <- (Kitchen (temp ?x&:(and (<= 22)(> 20))))
	(temp_out(value ?y&:(and (<= 20)(> 15))))
	=>
	(modify ?temp (ac off)(blind open)(heat off))
)

(defrule kthotmild
	?temp <- (Kitchen (temp ?x&:(and (<= 25)(> 22))))
	(temp_out(value ?y&:(and (<= 22)(> 20))))
	=>
	(modify ?temp (ac off)(blind open)(heat off))
)

(defrule kthotcold
	?temp <- (Kitchen (temp ?x&:(and (<= 25)(> 22))))
	(temp_out(value ?y&:(and (< 20)(> 15))))
	=>
	(modify ?temp (ac off)(blind open)(heat off))
)

(defrule kthothot
	?temp <- (Kitchen (temp ?x&:(and(<= 25)(> 22))))
	(temp_out(value ?y&:(and (<= 25)(> 22))))
	=>
	(modify ?temp (ac on)(blind closed)(heat off))
)

(defrule ktcoldcold
	?temp <- (Kitchen (temp ?x&:(and(<= 20)(> 15))))
	(temp_out(value ?y&:(and (<= 20)(> 15))))
	=>
	(modify ?temp (ac off)(blind open)(heat on))
)

(defrule ktcoldmild
	?temp <- (Kitchen (temp ?x&:(and(<= 15)(> 20))))
	(temp_out(value ?y&:(and(<= 22)(> 20))))
	=>
	(modify ?temp (ac off)(blind open)(heat on))
)

(defrule ktcoldhot
	?temp <- (Kitchen (temp ?x&:(and(<= 20)(> 15))))
	(temp_out(value ?y&:(and (<= 25)(> 22))))
	=>
	(modify ?temp (ac off)(blind open)(heat on))
)
;=============================================================================
(defrule wcmildmild
	?temp <- (WC (temp ?x&:(and(< 22)(> 20))))
	(temp_out(value ?y&:(and (< 22)(> 20))))
	=>
	(modify ?temp (ac off)(blind closed)(heat off))
)

(defrule wcmildhot
	?temp <- (WC (temp ?x&:(and(<= 22)(> 20))))
	(temp_out(value ?y&:(and(<= 25)(> 22))))
	=>
	(modify ?temp (ac off)(blind closed)(heat off))
)

(defrule wcmildcold
	?temp <- (WC (temp ?x&:(and(<= 22)(> 20))))
	(temp_out(value ?y&:(and(<= 20)(> 15))))
	=>
	(modify ?temp (ac off)(blind open)(heat off))
)

(defrule wchotmild
	?temp <- (WC (temp ?x&:(and(<= 25)(> 22))))
	(temp_out(value ?y&:(and(<= 22)(> 20))))
	=>
	(modify ?temp (ac off)(blind open)(heat off))
)

(defrule wchotcold
	?temp <- (WC (temp ?x&:(and (<= 25)(> 22))))
	(temp_out(value ?y&:(and(< 20)(> 15))))
	=>
	(modify ?temp (ac off)(blind open)(heat off))
)

(defrule wchothot
	?temp <- (WC (temp ?x&:(and (<= 25)(> 22))))
	(temp_out(value ?y&:(and(<= 25)(> 22))))
	=>
	(modify ?temp (ac on)(blind closed)(heat off))
)

(defrule wccoldcold
	?temp <- (WC (temp ?x&:(and (<= 20)(> 15))))
	(temp_out(value ?y&:(and (<= 20)(> 15))))
	=>
	(modify ?temp (ac off)(blind open)(heat on))
)

(defrule wccoldmild
	?temp <- (WC (temp ?x&:(and (<= 15)(> 20))))
	(temp_out(value ?y&:(and(<= 22)(> 20))))
	=>
	(modify ?temp (ac off)(blind open)(heat on))
)

(defrule wccoldhot
	?temp <- (WC (temp ?x&:(and(<= 20)(> 15))))
	(temp_out(value ?y&:(and(<= 25)(> 22))))
	=>
	(modify ?temp (ac off)(blind open)(heat on))
)
;=======================================================================================






